﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TicketingCompany
{
    public partial class Locations : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ButtonRefresh_Click(object sender, EventArgs e)
        {

        }

        protected void ButtonShowCreateLocationPanel_Click(object sender, EventArgs e)
        {
            PanelCreateLocation.Visible = !PanelCreateLocation.Visible;
        }

        protected void ButtonCreateLocation_Click(object sender, EventArgs e)
        {
            if (0 < TextBoxLocationName.Text.Length && TextBoxLocationName.Text.Length <= 50)
            {
                LabelErrorCreate.Visible = false;
                TicketingCompanyEntities dbContext = new TicketingCompanyEntities();
                using (dbContext)
                {
                    dbContext.Locations.AddObject(new Location { Name = TextBoxLocationName.Text });
                    dbContext.SaveChanges();
                    TextBoxLocationName.Text = "";
                    GridViewLocations.DataBind();
                }
            }
            else
            {
                LabelErrorCreate.Visible = true;
            }
        }

        protected void OnGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            if (!GetOperations.IsSafeToDeleteLocationWithID((int)e.Keys[0]))
            {
                e.Cancel = true;
                LabelDeleteInfo.Text = "It seems the location is still assosiated.";
            }
            else
            {
                LabelDeleteInfo.Text = "Delete successful.";
            }
        }

        protected void OnGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            string name = e.NewValues["Name"].ToString();
            if (1 > name.Length || name.Length > 50)
            {
                e.Cancel = true;
                LabelDeleteInfo.Text = "Invalid Name Lenght";
            }
            else
            {
                LabelDeleteInfo.Text = "Update successful.";
            }
        }

    }
}